package entity;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * Description:  后端返回给前端 统一jackson代码
 *
 * @Autor: Rolin
 * datetime: 05-12-8:59
 */
@Data
public class CommonResult {

    //此处所有的 成员变量 都是返回 给前端页面的 json字段
    @ApiModelProperty(value = "是否成功")
    private Boolean result;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<String, Object>();

    //    构造器私有化，该类不允许 被调用！
    private CommonResult() {

    }

    public static CommonResult success() {
        CommonResult commonResult = new CommonResult();
        commonResult.setResult(true);
        commonResult.setCode(ResultCode.SUCCESS);
        commonResult.setMessage("成功");
        return commonResult;
    }

    public static CommonResult error() {
        CommonResult commonResult = new CommonResult();
        commonResult.setResult(false);
        commonResult.setCode(ResultCode.ERROR);
        commonResult.setMessage("失败");
        return commonResult;
    }

    public CommonResult success(Boolean success) {
        this.setResult(success);
        return this;
    }

    public CommonResult message(String message) {
        this.setMessage(message);
        return this;
    }

    public CommonResult code(Integer code) {
        this.setCode(code);
        return this;
    }

    public CommonResult data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

//    此处在 该类中采用方法重载的方式 进行设计
    public CommonResult data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }
}
